﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Forums
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;

    internal partial class SendReceiveHelper<TConnection> where TConnection : Connection
    {
        private sealed class ReceiveContentsContext : FileOperationContext
        {
            private readonly long itemCount;

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            internal ReceiveContentsContext(SendReceiveContext context, IProgress<ISendReceiveProgress> progress) :
                base(context, progress)
            {
                this.itemCount = this.Root.GetContentsToReceiveCount(this.Group);
            }

            internal sealed override void SaveContent(IInboundArticleInfo article, Stream content)
            {
                base.SaveContent(article, content);
                ++this.ItemIndex;
            }

            internal void DeleteArticle(IInboundArticleInfo article)
            {
                var implementation = (InboundArticle)article;

                foreach (var followup in this.Root.GetFollowupInboundArticles(implementation))
                {
                    followup.MakeOriginal();
                }

                this.Root.DeleteOnSubmit(implementation);
                ++this.ItemIndex;
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            [SuppressMessage("Microsoft.Design", "CA1047:DoNotDeclareProtectedMembersInSealedTypes", Justification = "Override, CA bug?")]
            protected sealed override long ItemOffset
            {
                get { return 0; }
            }

            [SuppressMessage("Microsoft.Design", "CA1047:DoNotDeclareProtectedMembersInSealedTypes", Justification = "Override, CA bug?")]
            protected sealed override long ItemCount
            {
                get { return this.itemCount; }
            }

            [SuppressMessage("Microsoft.Design", "CA1047:DoNotDeclareProtectedMembersInSealedTypes", Justification = "Override, CA bug?")]
            protected sealed override ISendReceiveProgress CreateProgress(int percent)
            {
                return new ContentReceiveProgress(percent, this.Group.Name);
            }
        }
    }
}
